// ActionScript file
import com.flex.core.constants.GameConstants;
import com.flex.core.model.base.GameCanvas;
import com.flex.core.util.img.ImgUtils;

import flash.events.Event;
import flash.events.MouseEvent;
import flash.system.System;

import mx.collections.ArrayCollection;
import mx.containers.Canvas;
import mx.containers.Form;
import mx.containers.FormItem;
import mx.containers.VBox;
import mx.controls.Alert;
import mx.controls.Button;
import mx.controls.ComboBox;
import mx.controls.Text;
import mx.controls.TextInput;

/**
 * back to menu
 */
public function backToMenu(e:MouseEvent):void
{
	var canv:Canvas=GameConstants.BASE_ELE_MAP.gets("menu_info") as Canvas;
	canv.removeAllChildren();
	var parent:VBox=canv.parent as VBox;
	if (parent.numChildren > 1)
	{
		parent.removeChildAt(1);
	}
}

/**
 * show map list
 */
public function showMapList(e:MouseEvent):void
{
//	backToMenu(e);
//	var canv:Canvas=GameConstants.BASE_ELE_MAP.gets("menu_info") as Canvas;
//	var form:Form=new Form();
//	form.graphics.beginFill(0xFFFFFF, 1);
//	form.graphics.drawRect(0, 0, 400, 300);
//	form.graphics.endFill();
//	canv.addChild(form);
//
//	var formItem1:FormItem=new FormItem();
//	var text:Text=new Text();
//	text.text="目前已创建的地图列表如下";
//	formItem1.addChild(text);
//	form.addChild(formItem1);
//
//	var flag:Boolean=false;
//
//	var formItem2:FormItem=new FormItem();
//	formItem2.label="请选择要重新编辑的地图";
//
//	var fs:FileStream=new FileStream();
//	var file:File=new File(GameConstants.MAP_WRITE_PATH);
//	fs.open(file, FileMode.READ);
//	var str:String=fs.readUTFBytes(fs.bytesAvailable);
//	var mapArray:Array=new Array();
//	var cards:ArrayCollection=new ArrayCollection();
//	if (str != "" && str.replace(" ", "").length > 0)
//	{
//		mapArray=JSON.decode(str) as Array;
//		for (var i:Number=0; i < mapArray.length; i++)
//		{
//			var tempMap:Object=mapArray[i];
//			var temp:Object=new Object();
//			var mapPath:String=tempMap["mapPath"];
//			var array:Array=mapPath.split("/");
//			var name:String=array[array.length - 1];
//			temp["label"]=name;
//			temp["data"]=mapPath;
//			cards.addItem(temp);
//		}
//	}
//	if (cards.length > 0)
//	{
//		var combo:ComboBox=new ComboBox();
//		combo.dataProvider=cards;
//		formItem2.addChild(combo);
//		flag=true;
//	}
//	else
//	{
//		var text2:Text=new Text();
//		text2.text="尚未生成任何地图，请先去创建";
//		formItem2.label="";
//		formItem2.addChild(text2);
//	}
//	form.addChild(formItem2);
//
//	if (flag)
//	{
//		var formItem3:FormItem=new FormItem();
//		var btn:Button=new Button();
//		btn.label="查看地图详情";
//		btn.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
//		{
//			showMapInfoChange(e, combo.selectedItem.data);
//		});
//		formItem3.addChild(btn);
//		form.addChild(formItem3);
//
//	}
}

/**
 * show current changed map info
 */
public function showMapInfoChange(e:MouseEvent, imgPath:String):void
{
//	var canv:Canvas=menu_info;
//	var img:Mimage=ImgUtils.getImage(imgPath, "resizeMap");
//	canv.addChild(ImgUtils.getImage(imgPath, "resizeMap"));
	var vBox:Canvas=GameConstants.BASE_ELE_MAP.gets("menu_info") as Canvas;
	vBox.removeAllChildren();

	var canv:Canvas=new Canvas();
	vBox.addChild(canv);

	var bgCanv:Canvas=new Canvas();
	canv.addChild(bgCanv);

	var gridCanv:GameCanvas=new GameCanvas();
	canv.addChild(gridCanv);

	bgCanv.addChild(ImgUtils.getImage(imgPath, "resizeChangedMap"));
}

/**
 * create map
 */
public function createMap(e:MouseEvent):void
{
	System.gc();
	var canvas:GameCanvas=new GameCanvas();
	var menuInfo:Canvas=GameConstants.BASE_ELE_MAP.gets("menu_info") as Canvas;
	menuInfo.removeAllChildren();
	
	var parent:VBox=menuInfo.parent as VBox;
	if (parent.numChildren > 1)
	{
		parent.removeChildAt(1);
	}

	var form:Form=new Form();
	form.graphics.beginFill(0xFFFFFF, 1);
	form.graphics.drawRect(0, 0, 400, 300);
	form.graphics.endFill();

	var formItem2:FormItem=new FormItem();
	formItem2.label="请选择背景图";
	var cards:ArrayCollection=new ArrayCollection();
	for (var i:Number=0; i < GameConstants.BASE_IMG_LIST.length; i++)
	{
		var tempMap:Object=GameConstants.BASE_IMG_LIST[i];
		var temp:Object=new Object();
		var mapPath:String=tempMap["mapPath"];
		var array:Array=mapPath.split("/");
		var name:String=array[array.length - 1];
		temp["label"]=name;
		temp["data"]=mapPath;
		cards.addItem(temp);
	}
	var combo:ComboBox=new ComboBox();
	combo.dataProvider=cards;
//	combo.addEventListener(ListEvent.CHANGE,);//增加选择器的监听事件
//	var input:TextInput=new TextInput();
//	input.text="1";
	formItem2.addChild(combo);
	form.addChild(formItem2);

	var formItem4:FormItem=new FormItem();
	formItem4.label="请输入格子的总宽度";
	var input3:TextInput=new TextInput();
	input3.text=GameConstants.BASE_GRID_WIDTH + "";
	formItem4.addChild(input3);
	form.addChild(formItem4);

	var formItem5:FormItem=new FormItem();
	formItem5.label="请输入格子的总高度";
	var input4:TextInput=new TextInput();
	input4.text=GameConstants.BASE_GRID_HEIGHT + "";
	formItem5.addChild(input4);
	form.addChild(formItem5);

	var formItem7:FormItem=new FormItem();
	var btn2:Button=new Button();
	btn2.label="生成预览";
	btn2.addEventListener(MouseEvent.CLICK, function(e:MouseEvent):void
	{
		GameConstants.BASE_GRID_WIDTH=Number(input3.text);
		GameConstants.BASE_GRID_HEIGHT=Number(input4.text);
		createMapInfo(e, combo.selectedItem.data);
	});
	formItem7.addChild(btn2);
	form.addChild(formItem7);

	canvas.addChild(form);
	menuInfo.addChild(canvas);
}



public function createMapInfo(e:MouseEvent, data:String):void
{
	var vBox:Canvas=GameConstants.BASE_ELE_MAP.gets("menu_info") as Canvas;
	vBox.removeAllChildren();

	var canv:Canvas=new Canvas();
	vBox.addChild(canv);

	var bgCanv:Canvas=new Canvas();
	canv.addChild(bgCanv);

	var gridCanv:GameCanvas=new GameCanvas();
	canv.addChild(gridCanv);

	bgCanv.addChild(ImgUtils.getImage(data, "resizeMap"));
}

/**
 * read mapInfo from txt
 */
public function getHttpReqImg(e:Event):void
{
	try
	{
		var us:URLStream=e.target as URLStream;
		var xmldata:String=us.readMultiByte(us.bytesAvailable, "UTF-8");
		var obj:Array=JSON.decode(xmldata) as Array;
		for (var i:Number=0; i < obj.length; i++)
		{
			var temp:Object=obj[i];
			GameConstants.BASE_IMG_LIST.addItem(temp);
		}
	}
	catch (err:Error)
	{
		Alert.show("request error:" + err.message);
	}
}